using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class EntityDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public EntityDA()
		{
		}

		public EntityDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Entity table.
		/// </summary>
		public  int Insert(Entity entity)
		{
			ValidationUtility.ValidateArgument("entity", entity);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", entity.TenantID),
				new SqlParameter("@EntityName", entity.EntityName)
			};

			entity.EntityID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "EntityInsert", parameters));
			return entity.EntityID;
		}

		/// <summary>
		/// Updates a record in the Entity table.
		/// </summary>
		public  int Update(Entity entity)
		{
			ValidationUtility.ValidateArgument("entity", entity);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@EntityID", entity.EntityID),
				new SqlParameter("@TenantID", entity.TenantID),
				new SqlParameter("@EntityName", entity.EntityName)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "EntityUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the Entity table by its primary key.
		/// </summary>
		public  int Delete(int entityID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@EntityID", entityID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "EntityDelete", parameters));
		}

		/// <summary>
		/// Deletes a record from the Entity table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "EntityDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects a single record from the Entity table.
		/// </summary>
		public  Entity Select(int entityID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@EntityID", entityID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "EntitySelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakeEntity(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the Entity table.
		/// </summary>
		public  List<Entity> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "EntitySelectAll", parameters))
			{
				List<Entity> entityList = new List<Entity>();
				while (dataReader.Read())
				{
					Entity entity = MakeEntity(dataReader);
					entityList.Add(entity);
				}

				return entityList;
			}
		}

		/// <summary>
		/// Selects all records from the Entity table by a foreign key.
		/// </summary>
		public  List<Entity> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "EntitySelectAllByTenantID", parameters))
			{
				List<Entity> entityList = new List<Entity>();
				while (dataReader.Read())
				{
					Entity entity = MakeEntity(dataReader);
					entityList.Add(entity);
				}

				return entityList;
			}
		}

		/// <summary>
		/// Creates a new instance of the Entity class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  Entity MakeEntity(SqlDataReader dataReader)
		{
			Entity entity = new Entity();
			entity.EntityID = SqlClientUtility.GetInt32(dataReader, "EntityID", 0);
			entity.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			entity.EntityName = SqlClientUtility.GetString(dataReader, "EntityName", String.Empty);

			return entity;
		}

		#endregion
	}
}
